package com.sophialex.health.record.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.Constants;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.lktable.LKTable;
/**
 * 取消家庭关系
 * <p>Title:Health</p>
 * <p>CancelFamilyRelationAction</p>
 * 
 * Alex Apr 24, 2011 12:10:46 PM create
 * @author <a href='wanghao924@126.com'>Alex</a>
 * QQ: 873987060;
 * @version 1.0
 */
public class CancelFamilyRelationAction extends Action implements IWorkbenchAction {
    public static final String ID = "com.sophialex.health.record.action.CancelFamilyRelationAction";
    
    private IWorkbenchWindow workbenchWindow;
    private LKTable showTable;
    private List<HrPerson> hrPersonList;
    


	public CancelFamilyRelationAction(IWorkbenchWindow workbenchWindow,LKTable showTable,List hrPersonList){
    	if(workbenchWindow==null){
    		throw new IllegalArgumentException();
    	}
    	this.showTable = showTable;
    	this.hrPersonList = hrPersonList==null?new ArrayList<HrPerson>():hrPersonList;
    	this.workbenchWindow = workbenchWindow;
    	setId(ID);
    	setText(Messages.getString("CancelFamilyRelationAction.0"));
    	setImageDescriptor(ImageUtil.cancel_relation);
    }
    
	public void dispose() {
		workbenchWindow = null;
	}
	
	public void run(){
		if(workbenchWindow == null){
			return;
		}
		HrPerson currentPerson = SuperContext.currentPerson;
		if(currentPerson == null){
			DialogManager.invokeWarnDlg(Messages.getString("Common.12"));
			return;
		}
		if (showTable.getSelection() != null) {
			HrPerson hrPerson = (HrPerson) showTable.getSelection();
			if(hrPerson.getPerRelaMaster()!= null && hrPerson.getPerRelaMaster().longValue() == Constants.HZ.longValue()){
				DialogManager.invokeInfoDlg(Messages.getString("CancelFamilyRelationAction.4"));
				return;
			}
			if(!DialogManager.invokeConfirmDlg(Messages.getString("CancelFamilyRelationAction.7"))){
				return;
			}
			try {
				DataSource.hrPersonService.cancelRelation(hrPerson.getId());
				hrPersonList.remove(hrPerson);
				showTable.setInput(hrPersonList);
				DialogManager.invokeInfoDlg(Messages.getString("CancelFamilyRelationAction.2"));
			}
			catch(Exception e) {
				DialogManager.invokeInfoDlg(Messages.getString("CancelFamilyRelationAction.3"));  
			}
		}else{
			DialogManager.invokeInfoDlg(Messages.getString("CancelFamilyRelationAction.1"));
			
		}
	}
    public void setHrPersonList(List<HrPerson> hrPersonList) {
		this.hrPersonList = hrPersonList;
	}

}
